let { list } = require("./mock");
const bodyParser = require("body-parser");
module.exports = (app) => {
  app.use(bodyParser.json());
  app.post("/api/list", (req, res) => {
    const { type, value, dates } = req.body;

    const newList = list.filter((v) => {
      if (type === "全部") return true;
      return type === v.type;
    }).filter(v => {
      return v.title.includes(value)
    }).filter(v => {
      if(!dates) return true
      if(dates.length === 0) return true
      return dates[0] < new Date(v.createDate).getTime() &&  new Date(v.createDate).getTime() < dates[1]
    })

    res.send({
      code: 200,
      data: newList,
    });
  });

  app.post("/api/list/delete", (req, res) => {
    const { id } = req.body;
    list = list.filter((v) => v.id !== id);
    res.send({
      code: 200,
      message: "删除！",
    });
  });

  app.post("/api/list/delete/checked", (req, res) => {
    const { ids } = req.body;
    list = list.filter((v) => !ids.includes(v.id));
    res.send({
      code: 200,
      message: "删除！",
    });
  });
};
